# -*- coding: utf-8 -*-
# @Time : 2021/4/25 21:30
# @Author : lucy
# @Email : lilu7@asiainfo.com
# @File : 登录注册.py
# @Project : basic_gramer

import pymysql


class Register(object):
    conn = pymysql.connect('49.233.39.160', 'user', 'leboAa!#$123', 'lebo16', 3306)
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

    def register(self):
        while True:
            username = input("请输入姓名:")
            password = input("请输入密码：")

            sql = """
            select * from students17 where name = %s
            """
            count = self.cursor.execute(sql, [username])
            print(count)
            if count == 0:
                sql = """
                insert into students17(name,card) values(%s, %s)
                """
                self.cursor.execute(sql, [username, password])
                print(sql)
                self.conn.commit()
            else:
                print('用户名%s已存在，请重新输入进行注册' % username)
                continue

    def login(self):
        while True:
            username = input("请输入姓名: ")
            password = input("请输入密码：")
            sql = """
            select name,card from students17 where name = %s
            """
            self.cursor.execute(sql, username)
            res = self.cursor.fetchone()
            print(res)
            for key in res:
                print(key, res[key])
                if res['name'] == username and res['card'] == password:
                    print("登录成功")
                else:
                    print('用户名或者密码错误，请重新输入')
                    continue


regist = Register()
regist.register()
regist.login()
